package com.sdhs.paas.authserver.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdhs.paas.authserver.entity.UserInfo;
import com.sdhs.paas.authserver.service.AuthService;
import com.sdhs.paas.authserver.service.UserService;
import com.sdhs.paas.authserver.util.user.JwtAuthenticationRequest;
import com.sdhs.paas.authshare.common.msg.ObjectRestResponse;
import com.sdhs.paas.authshare.util.jwt.IJWTInfo;
import com.sdhs.paas.sdk.core.logger.Logger;
import com.sdhs.paas.sdk.core.logger.LoggerFactory;

@RestController
@RequestMapping("jwt")
public class AuthController {
	private static Logger logger  = LoggerFactory.getLogger(AuthController.class); 
	
    @Value("${jwt.token-header}")
    private String tokenHeader;

    @Autowired
    private AuthService authService;
    

    @RequestMapping(value = "token", method = RequestMethod.POST)
    public ResponseEntity<String> createAuthenticationToken(
            @RequestBody JwtAuthenticationRequest authenticationRequest) throws Exception {
        final String token = authService.login(authenticationRequest);
        logger.info(token);
        return new ResponseEntity<>(token,HttpStatus.OK);
    }
    
    @RequestMapping(value = "ntoken", method = RequestMethod.POST)
    public ObjectRestResponse<String> createAuthenticationTokenN(
            @RequestBody JwtAuthenticationRequest authenticationRequest) throws Exception {
        final String token = authService.login(authenticationRequest);
        logger.info(token);
        return new ObjectRestResponse<>().data(token);
    }
    //根据token获取用户信息
    @RequestMapping(value = "userinfo", method = RequestMethod.GET)
    public ObjectRestResponse<UserInfo> getUserInfo(
            @RequestParam(value = "token", required = true) String token) throws Exception {
    	UserInfo jwtInfo = authService.getUserInfo(token);
        return new ObjectRestResponse<>().data(jwtInfo);
    }
    
    @RequestMapping(value = "refresh", method = RequestMethod.GET)
    public ResponseEntity<String> refreshAndGetAuthenticationToken(
            HttpServletRequest request) throws Exception {
        String token = request.getHeader(tokenHeader);
        String refreshedToken = authService.refresh(token);
        logger.info("token---"+token+"  refreshedToken--"+refreshedToken);
        return new ResponseEntity(refreshedToken,HttpStatus.OK);
    }

    @RequestMapping(value = "verify", method = RequestMethod.GET)
    public ResponseEntity<?> verify(String token) throws Exception {
    	IJWTInfo jwtInfo = authService.validate(token);
//        authService.validate(token);
        logger.info(token);
        return new ResponseEntity(jwtInfo,HttpStatus.OK);
    }
}
